package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.CamelUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.domain.SysTableColumn;
import com.ruoyi.system.domain.TbCompany;
import com.ruoyi.system.domain.vo.ColumnQueryVo;
import com.ruoyi.system.domain.vo.ProtectType;
import com.ruoyi.system.mapper.TbCompanyMapper;
import com.ruoyi.system.service.ISysTableColumnService;
import com.ruoyi.system.service.ISysTableService;
import com.ruoyi.system.service.ITbCompanyService;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class TbCompanyServiceImpl implements ITbCompanyService {

    @Autowired
    private TbCompanyMapper tbCompanyMapper;
    @Autowired
    private ISysTableColumnService sysTableColumnService;
    @Autowired
    private ISysTableService sysTableService;


    @Override
    public Map save(Map<String, Object> data) {
        TbCompany entity = new TbCompany();
        entity.genField(data);
        tbCompanyMapper.save(entity);
        data = entity.toMap();
        return data;
    }

    @Override
    public Map update(Map<String, Object> data) {
        TbCompany entity = new TbCompany();
        entity.genField(data);
        tbCompanyMapper.update(entity);
        data = entity.toMap();
        return data;
    }

    @Override
    public Map getById(Long id, String tableName) {
        Map<String, Object> data = tbCompanyMapper.selectById(id, tableName);
        CamelUtil.formatTime(data);
        return data;
    }

    @Override
    public List<?> list(Map<String, Object> params, Long tableId) {
        params.put("page", null);
        params.put("limit", null);
        params.put("pageNum", null);
        params.put("pageSize", null);
        SysTable table = sysTableService.selectSysTableById(tableId);
        SysTableColumn queryColumn = new SysTableColumn();
        queryColumn.setTableId(tableId);
        List<SysTableColumn> columns = sysTableColumnService.selectSysTableColumnList(queryColumn);

        // 查询条件
        List<ColumnQueryVo> queryVos = Lists.newArrayList();

        Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            String defQueryType = "";
            if(key.contains("__FILTER__")) {
                key = entry.getKey().split("__FILTER__")[0];
                defQueryType = entry.getKey().split("__FILTER__")[1];
            }
            key = StringUtils.toUnderScoreCase(key);// 下划线
            Object value = entry.getValue();
            // 有值
            if(value != null && !"".equals(value.toString())) {
                ColumnQueryVo temp = new ColumnQueryVo();

                String queryType = "EQ"; // 默认EQ
                String columnType = "";

                for (SysTableColumn column : columns) {
                    String name = column.getColumnName();
                    if(key.equals(name)) {
                        queryType = column.getQueryType();
                        columnType = column.getColumnType();
                        break;
                    }
                }

                // 有参数配置的，优先级更高
                if(!StringUtils.isEmpty(defQueryType)) {
                    queryType = defQueryType;
                } else if(StringUtils.isEmpty(queryType)) {
                    queryType = "EQ";
                }

                // 时间类型查询条件
                if(!StringUtils.isEmpty(columnType) && columnType.indexOf("date") != -1) {
                    key = "date_format(" + key + ", '%Y-%m-%d')";
                }
                temp.setColumnName(key);
                temp.setQueryValue(value);
                temp.setQueryType(queryType);
                queryVos.add(temp);
            }
        }

        List<Map> list = tbCompanyMapper.selectList(queryVos, table.getTableName());

        for (Map map : list) {
            CamelUtil.formatTime(map);
        }
        return list;
    }

    @Override
    public int delete(Long[] ids, Long tableId) {
        SysTable table = sysTableService.selectSysTableById(tableId);
        for (Long id : ids) {
            tbCompanyMapper.deleteById(id, table.getTableName());
        }
        return ids.length;
    }

    @Override
    public void delete(Long id, String tableName) {
        tbCompanyMapper.deleteById(id, tableName);
    }


}
